﻿/****************
 * Copyright 2016 Brian Tabone
 * Licensed under the Apache 2.0 License
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Property.cs
 * The main business logic for Property management
 * 8/27/2016
 */

using System;
using IAO.Common.Network.MessageImplementations.SCADA;

namespace IAO.Common.ServiceFramework.RuntimeProperties
{
    public class Property
    {
        public Property()
        {
        }

        public Property(int id, string classStr, string name, string value, string type, bool isReadOnly)
        {
            Id = id;
            Class = classStr;
            Name = name;
            Value = value;
            Type = type;
            ReadOnly = isReadOnly;
        }

        /// <summary>
        ///     Static properties just call this but dynamic properites should override this and get current state
        /// </summary>
        /// <returns></returns>
        public PropertyMessage Message()
        {
            return new PropertyMessage
            {
                Id = Id,
                Class = Class,
                Name = Name,
                Type = Type,
                Value = Value,
                ReadOnly = ReadOnly
            };
        }

        public virtual void SetValue(string value)
        {
            if (ReadOnly)
            {
                throw new NotImplementedException($"{Id}:{Name} is readonly");
            }

            switch (Type)
            {
                case "System.String":
                    break;
                case "System.Double":
                    double result;
                    if (!double.TryParse(value, out result))
                    {
                        throw new ArgumentException($"{value} is not a double");
                    }
                    break;
                case "System.Int32":
                    int intResult;
                    if (!int.TryParse(value, out intResult))
                    {
                        throw new ArgumentException($"{value} is not an integer");
                    }
                    break;
                default:
                    break;
            }

            // Value format test complete, set value
            Value = value;

            // Subclasses should overide this method and perform their actions once the value has been set
        }

        #region PropertyMessage properties

        /// <summary>
        ///     Integer ID for this property, must be unique
        /// </summary>
        public int Id { get; protected set; }

        /// <summary>
        ///     The class (in namespace format, though this is not enforced by the code) for this property. A class is how
        ///     properties are grouped and their
        ///     hiearchy is managed
        /// </summary>
        public string Class { get; protected set; }

        /// <summary>
        ///     String name of this property
        /// </summary>
        public string Name { get; protected set; }

        /// <summary>
        ///     The value of this property (serialized as a string)
        /// </summary>
        public virtual string Value { get; protected set; }

        /// <summary>
        ///     THe data type of this property
        /// </summary>
        public string Type { get; protected set; }

        /// <summary>
        ///     Whether or not this property can be written
        /// </summary>
        public bool ReadOnly { get; protected set; }

        #endregion
    }
}